package transaction;

import order.OrderBuy;
import order.OrderSale;
import order.OrderType;
import order.UnderdoneOrder;

public class Transaction {
    private final long saleId;
    private final long buyId;
    private final long remainderId;

    private final String saleClientName;
    private final String buyClientName;
    private final String remainderClientName;

    private final double salePrice;
    private final double buyPrice;
    private final double transactionPrice;

    private final int transactionSize;
    private final int transactionRemainderSize;

    private final OrderType remainderOrderType;


    public Transaction(final OrderBuy buy, final OrderSale sale) {
        saleId = sale.getId();
        buyId = buy.getId();

        saleClientName = sale.getClientName();
        buyClientName = buy.getClientName();

        salePrice = sale.getPrice();
        buyPrice = buy.getPrice();
        transactionPrice = sale.getPrice();

        transactionSize = Math.min(buy.getSize(), sale.getSize());
        transactionRemainderSize = Math.abs(buy.getSize() - sale.getSize());

        if (buy.getSize() > sale.getSize()) {
            remainderOrderType = OrderType.BUY;
            remainderId = buyId;
            remainderClientName = buyClientName;
        } else if (buy.getSize() < sale.getSize()) {
            remainderOrderType = OrderType.SALE;
            remainderId = saleId;
            remainderClientName = saleClientName;
        } else {
            remainderOrderType = null;
            remainderClientName = null;
            remainderId = -1;
        }
    }

    public UnderdoneOrder getRemainderOrder() {
        if (remainderOrderType == null) {
            return null;
        }
        return new UnderdoneOrder(transactionPrice, transactionRemainderSize,
                remainderOrderType, remainderClientName);
    }

    public long getSaleId() {
        return saleId;
    }

    public long getBuyId() {
        return buyId;
    }

    public String getBuyClientName() {
        return buyClientName;
    }

    public String getSaleClientName() {
        return saleClientName;
    }

    public int getTransactionSize() {
        return transactionSize;
    }

    public long getRemainderId() {
        return remainderId;
    }
}
